from itertools import accumulate
from typing import List


class Solution:
    def maxFrequency(self, nums: List[int], k: int) -> int:
        nums.sort()
        prefix = [0] + list(accumulate(nums))
        ans = 0
        l = 0
        n = len(nums)
        for r in range(n):
            while (r-l)*nums[r] - (prefix[r]-prefix[l]) > k:
                l += 1
            ans = max(ans, r - l + 1)
        return ans

if __name__ == '__main__':
    s = Solution()
    r = s.maxFrequency(nums = [1,2,4], k = 5)
    print(r)
